#ifndef DRV922XX_TRAPEZOIDAL_SENSORED_H_
#define DRV922XX_TRAPEZOIDAL_SENSORED_H_

#include "DRV922XX.h"
#include "DRV922XX_Utils.h"

#define MSP_CONTROL 0
#define TEST_WITH_NO_CURRENT_SAMPLING 0

// Definitions

#define UI_BRAKE_APPLIED		((P4IN & 0x20) == 0)
#define BRAKE_ON				1
#define BRAKE_OFF				0
extern unsigned int brakeState; 
extern unsigned int isMoving; 
#define IS_MOVING	1
#define IS_NOT_MOVING 0
#define UI_REVERSE_BTN_APPLIED  ((P4IN & 0x40) != 0)
#define ADC_CHANNEL_SPEEDIN   		8										/*< Throttle speed input ADC channel */
#define ADC_CHANNEL_CS        	14  									/*!< ADC15 */

#define CCR_PERIOD_TIME				780										/*!< CCR_MAX * 2 [up/down counter] * 1/25MHz = 16kHz PWM */

#define CCR_TICKS_1US				25										/*!< 1 us */
#define CCR_TICKS_3US				75										/*!< 3 us */
#define CCR_TICKS_4US	        	140 									/*!< 4 us */

#define CCR_TICKS_1MS			391										/*!< 1ms in slow timer ticks */
#define CCR_TICKS_2MS			782										/*!< 2ms in slow timer ticks */
#define CCR_TICKS_4MS			1564									/*!< 4ms in slow timer ticks */
#define CCR_TICKS_8MS			3128									/*!< 8ms in slow timer ticks */
#define CCR_TICKS_12MS			4692									/*!< 12ms in slow timer ticks */
#define CCR_TICKS_16MS			6256									/*!< 16ms in slow timer ticks */

// PWM Definitions
#define CCR_MAX					780										/*!< CCR_MAX * 2 [up/down counter] * 1/25MHz = 16kHz PWM. Max resolution */

#define ADC_PWR2   				10										/*!< log2(max ADC Value + 1) */

// This inidcates How much to advance the PWM center and falling edge ISRs
#define TA2_CCR_3US_COUNTS      75 										/*!< 3 usecs */

#define SENSE_GAIN_0				0x60									/*!< Setting of GAIN[1:0] bits for low-speed BEMF operation */
#define SENSE_GAIN_5k				0x40									/*!< Setting of GAIN[1:0] bits for mid-speed BEMF operation */
#define SENSE_GAIN_2p5k				0x00									/*!< Setting of GAIN[1:0] bits for high-speed BEMF operation */

#define TS_SHIFT_3p75_DEG			3										/*!< Amount of shift to get 3.75 degree for zero cross timing */
#define TS_SHIFT_7p5_DEG			2										/*!< Amount of shift to get 7.5 degree for zero cross timing */
#define TS_SHIFT_15_DEG				1										/*!< Amount of shift to get 15 degree for zero cross timing */
#define TS_SHIFT_30_DEG				0										/*!< Amount of shift to get 30 degree for zero cross timing */

// Misc Variables

extern unsigned char led;													/*!< LED enabled if set to 1 */
extern unsigned char sense_gain_value;										/*!< Sensoreless feedback resistor gain value */
extern unsigned int current_p3in_value;										/*!< Current value of P3IN used to track comparator state changes */
extern unsigned int last_p3in_value;										/*!< Last recorded value of P3IN */
extern unsigned int speed_timestamp_last;									/*!< Last speed timestamp */
extern unsigned char bemf_startup_cycles_completed;							/*!< Number of PWM periods since entering BEMF mode */
extern unsigned char zc_num_crosses;										/*!< Number of times that the ZC comparator output transitions - for use with digital filter */
extern unsigned char hunt_blanking_counts;									/*!< Number of blanking counts executed since entering BEMF mode */

extern volatile unsigned char throttle_voltage_high;						/*!< used to store MSB of throttle voltage when reading ADC (10b result) */
extern volatile unsigned char throttle_voltage_low;						/*!< used to store LSB of throttle voltage when reading ADC (10b result) */

extern volatile unsigned char MaxiCurrent_high;							/*!< used to store MSB of max current when reading ADC (10b result) */
extern volatile unsigned char MaxiCurrent_low;								/*!< used to store LSB of max current when reading ADC (10b result) */

extern volatile char saved_com_state; 									/*!< initial state will be guaranteed to mismatch */
extern unsigned char new_com_state;   									/*!< used to store current com state in update_com() */
extern unsigned char comm_tbl_index;											/*!< last known commutator table index */
extern unsigned char update_commutator;	
extern unsigned int timeOffSec;
extern unsigned int speed; 
extern unsigned int speedFiltered;
extern unsigned int pwmTicks;

// PWM support
extern unsigned char complementary_pwm;									/*!< Set to 1 when PWM in complementary mode */
extern volatile unsigned int pwm_duty;										/*!< used to store calculated PWM duty cycle */
extern unsigned char complementary_pwm_saved;								/*!< Last sytstem setting of complementary PWM mode */
extern unsigned char update_pwm_flag;										/*!< Indicates time to update PWM duty cycles */
extern void pwm_enable(unsigned char comm_tbl_index);												/*!< Enables PWM output via selecting Timer mode */
extern void pwm_disable(void);												/*!< Disables PWM output via selecting IO mode */
extern void pwm_drain_charge(void);
extern void pwm_blink_led(void);
extern unsigned int throttle_voltage_to_pwm(unsigned int throttle_voltage_f2);			/*!< Converts voltage to PWM duty cycle (timer ticks) */
extern void set_duty(void);

// Direction and Brake
extern unsigned int getTargetDirection(void); 
extern unsigned int directionChangeDetector(void);
extern void directionChangeHandler(void);
extern unsigned int getBrakeState(void);
extern int target_drive_direction;
extern int reverse_drive;
extern int just_changed_direction;
#define DIR_STOP 0
#define DIR_FORWARD 1
#define DIR_REVERSE 2

// flags and states
extern int process1msFlag; 
extern int process10msFlag;
extern int sample1msADCsFlag; 
extern int hallIsrFlag; 
extern int mode;
#define MODE_OFF 				0
#define MODE_ON  				1 
#define MODE_DIRECTION_CHANGE 	2

// Commutation
extern unsigned char current_com_index;										/*!< Current commutator table index for Comm_Values[] */
extern const unsigned char Comm_Values[];									/*!< Commutator values for each state */
extern const unsigned char Comm_Values_Align[];								/*!< Commutator values for each state when in Align and Go mode */
extern const unsigned char Reverse_Comm_Values[];									/*!< Commutator values for each state */
extern const unsigned char Reverse_Comm_Values_Align[];								/*!< Commutator values for each state when in Align and Go mode */
extern void update_com(void);												/*!< Updates the commutator according to \acurrent_com_index */

// Other functions
extern void sample_peak_current_setup(void);
extern void sample_peak_current(void);											/*!< Samples current and stores max current in \a AppMaxCurrent */
extern void sample_throttle(void);											/*!< Samples throttle and stores filtered in \a AppThrottleVoltageFiltered */
extern void sample_temperature(void);
extern void throttle_filter(void);
extern unsigned int throttleGetState(void);
extern void throttle_reset(void);
#define THROTTLE_ON	 1
#define THROTTLE_OFF 0
//extern void enter_align_go_mode(void);										/*!< Enters align and go mode */

// State machines

//extern void ipd_state_machine(void);										/*!< Initial Position Detection state machine */
//extern void align_state_machine(void);										/*!< Align and go state machine */
//extern void bemf_state_machine(void);										/*!< BEMF state machine */

#endif /*DRV922XX_TRAPEZOIDAL_SENSORED_H_*/
